var isInitBuffer = false;
var lastProcess = 0;
var playMode = 4;

function play(data){
	if(_isAli){
		IMPlayer.Stop();
	}
	
	
	showBox(data);
	isInitBuffer = true;
	lastPercent = 0;
	
	
	try {	
		IMPlayer.Play(data.playUrl,playMode);
	}catch (e) {
        $.log(e);
	}
}

function OnStateChanged(state, percent,speed){
	myPlayer.state = state;  
	if ("play" == state) {
		if(isInitBuffer){
			hideBox();
			hideBackground(); 
			isInitBuffer = false;
		}
	}else if("buffer" == state){
		if(isInitBuffer){
			if(percent > lastProcess){lastProcess = percent;}
			changeBox(lastProcess);
		}else{
		
		}
	}else if("end" == state){
		curItemId ++ ;
		if(dataList && dataList.length >= curItemId){
			var data = dataList[curItemId - 1];
			play(data);
		}else{
			KEY.KEY_RETURN();
		}
	}
}

function changeBox(per){
	if(per>0){
		$('#box .tip').html(lang.play.download);
	}
	$('#box .buffer').html(per+'%');
}

function showBox(curData){
	var title = curData.title;
	
	var img = $.getImageUrl(curData.img,DEFAULT_IMG);
	
	var con = '';
	con += '<div class="title">'+title+'</div>';
	con += '<div class="icon"><img src="'+img+'"/></div>';
	con += '<div class="tip">'+lang.play.init+'</div>';
	con += '<div class="buffer">0%</div>';
	
	$('#playInfo').html(con);
	$('#playBox').show();
}

function hideBox(){
	$('#box').hide();
}


var hasHideBackground = false;
function hideBackground(){
	if(!hasHideBackground){
		
		$('#pagewrap').attr("style","background: url();").hide();
		hasHideBackground = true;
	}
}

